package com.blueocean_health.care.management.service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.blueocean_health.care.management.em.OrderFlagEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.domain.base.OrderInfo;
import com.blueocean_health.care.management.em.OrderStatusEnum;

/**
 * 定时任务改变订单状态
 * @Author ChenPeng cp
 */
@Service
public class ScheduleService {

	@Autowired
	private OrderInfoService orderInfoService;
	  @Value("${sys.schedule}")
		private boolean schedule;
	//使用cron表达式
	@Scheduled(cron=" 0 0/30 * * * *")//每隔30分钟执行一次
	public void schedule(){
		if(!schedule){
			return;
		}
		//注意, 如果方法中的程序在周期内(30m)内无法执行完成, 会开启两次, 自行控制
		System.err.println("当前日期：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));

		Long now = System.currentTimeMillis();
		List<OrderInfo> lastOrderInfoList = orderInfoService.getLastOrderInfoList();

		for (int i = lastOrderInfoList.size() - 1; i >= 0; i--) {
			OrderInfo orderInfo =  lastOrderInfoList.get(i);

			String orderId = orderInfo.getOrderId();
			Long version = orderInfo.getVersion();
			Integer id = orderInfo.getId();

			try {
				Long first = orderInfoService.getFirstOrderTime(orderId,version);
				Long last = orderInfoService.getLastOrderTime(orderId,version);
				if (OrderFlagEnum._1.getKey().equals(orderInfo.getDeflag())) {
					//订单已删除的不去管
//					System.out.println("============id:"+id+"  订单号:"+orderId+"  版本号:"+version+" ============ 此订单已删除，跳过修改");
				}else if (OrderStatusEnum._8.getKey().equals(orderInfo.getOrderStatus())) {
					//订单退款中的不去管
//					System.out.println("============id:"+id+"  订单号:"+orderId+"  版本号:"+version+" ============ 此订单为退款中，跳过修改");
				}
				else if (OrderStatusEnum._6.getKey().equals(orderInfo.getOrderStatus())) {
					//订单已取消的不去管
//					System.out.println("============id:"+id+"  订单号:"+orderId+"  版本号:"+version+" ============ 此订单已取消，跳过修改");
				}

/*				else if (OrderStatusEnum._5.getKey().equals(orderInfo.getOrderStatus())) {
					//订单已完成的不去管
//					System.out.println("============id:"+id+"  订单号:"+orderId+"  版本号:"+version+" ============ 此订单已完成，跳过修改");
				} */


				else if (first == null || last == null) {
//					System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + "  ============ 无服务时间信息");
				} else if (first.equals(last)) {
//					System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + "  ============ 开始结束时间一样为用户端提交的待派单订单，不用管");
				} else if (now < first) {
					//当前时间小于开始时间  待服务
                    //若为退款中or已退款 则不改动
                    if (OrderStatusEnum._4.getKey().equals(orderInfo.getOrderStatus()) || OrderStatusEnum._7.getKey().equals(orderInfo.getOrderStatus())) {
//                        System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + " ============ 此订单为退款中，跳过修改");
                    }else{
                          //逆向改回待派单的不需要了
						 //orderInfoService.updateOrderInfoStatus(OrderStatusEnum._2.getKey(), id);
//                        System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + " ============ 订单状态修改成功");
                    }
				} else if (now >= first && last >= now) {
					//当前时间大于开始时间小于结束时间 服务中
					//若为退款中or已退款 则不改动
					if (OrderStatusEnum._4.getKey().equals(orderInfo.getOrderStatus()) || OrderStatusEnum._7.getKey().equals(orderInfo.getOrderStatus())) {
//						System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + " ============ 此订单为退款中，跳过修改");
					}else{
						orderInfoService.updateOrderInfoStatusForSchedule(OrderStatusEnum._3.getKey(), id);
						System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + " ============ 订单状态修改成功-服务中");
					}
				} else if (now > last) {
					//当前时间大于结束时间 已完成服务  若为退款中or已退款  则不改动
					if (OrderStatusEnum._4.getKey().equals(orderInfo.getOrderStatus()) || OrderStatusEnum._7.getKey().equals(orderInfo.getOrderStatus())) {
//						System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + " ============ 此订单为退款中，跳过修改");
					} else {
						orderInfoService.updateOrderInfoStatusForSchedule(OrderStatusEnum._5.getKey(), id);
						System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + " ============ 订单状态修改成功-已完成");

						//不再自动触发已完成，所有的已完成改为手动触发
						//System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + " ============ 订单已到结束时间，可以手动触发");
					}
				} else {
//					System.out.println("============id:" + id + "  订单号:" + orderId + "  版本号:" + version + " ============ 没有找到符合条件的选项");
				}
			} catch (Exception e) {
				e.printStackTrace();
//				System.out.println("============id:"+id+"  订单号:"+orderId+"  版本号:"+version+" ============ 订单状态修改失败");
			}

		}
	}

}
